package com.example.tests;

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;

public class TestBase {

	private static WebDriver driver;
	private static String baseUrl;
	private static StringBuffer verificationErrors = new StringBuffer();

	@BeforeTest
	public void setUp() throws Exception {
		driver = new FirefoxDriver();
		baseUrl = "http://localhost/";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}
	
	@AfterTest
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
	
	protected void returnToGroupsPage() {
	driver.findElement(By.linkText("group page")).click();
	}

	protected void submitGroupCreation() {
	driver.findElement(By.name("submit")).click();
	}

	protected void fillGroupForm(GroupData group) {
	driver.findElement(By.name("group_name")).clear();
	driver.findElement(By.name("group_name")).sendKeys(group.name);
	driver.findElement(By.name("group_header")).clear();
	driver.findElement(By.name("group_header")).sendKeys(group.header);
	driver.findElement(By.name("group_footer")).clear();
	driver.findElement(By.name("group_footer")).sendKeys(group.footer);
	}

	protected void initNewGroupCreation() {
	driver.findElement(By.name("new")).click();
	}

	protected void openGroupsPage() {
	driver.findElement(By.linkText("groups")).click();
	}

	protected void openMainPage() {
	driver.get(baseUrl + "/addressbookv4.1.4/");
	}

	
	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	protected void clickonHomePage() {
			// Click oh Home Page
			driver.findElement(By.linkText("home page")).click();
	}

	protected void clickonSubmit() {
			// Click on Submit
			driver.findElement(By.name("submit")).click();
	}

	protected void fillContact() {
		// Fill Contact 
		driver.findElement(By.name("firstname")).clear();
		driver.findElement(By.name("firstname")).sendKeys("Azamat");
		driver.findElement(By.name("lastname")).clear();
		driver.findElement(By.name("lastname")).sendKeys("Azemkulov");
		driver.findElement(By.name("address")).clear();
		driver.findElement(By.name("address")).sendKeys("17 10 Let Oktyabrya, Izhevsk, Udmurtiya, RF");
		driver.findElement(By.name("home")).clear();
		driver.findElement(By.name("home")).sendKeys("123-456-7890");
		driver.findElement(By.name("mobile")).clear();
		driver.findElement(By.name("mobile")).sendKeys("123-456-7890");
		driver.findElement(By.name("work")).clear();
		driver.findElement(By.name("work")).sendKeys("123-456-7890");
		driver.findElement(By.name("email")).clear();
		driver.findElement(By.name("email")).sendKeys("aza.azem@gmail.com");
		driver.findElement(By.name("email2")).clear();
		driver.findElement(By.name("email2")).sendKeys("aza.azem@gmail.com");
		new Select(driver.findElement(By.name("bday"))).selectByVisibleText("3");
		new Select(driver.findElement(By.name("bmonth"))).selectByVisibleText("March");
		driver.findElement(By.name("byear")).clear();
		driver.findElement(By.name("byear")).sendKeys("1977");
		new Select(driver.findElement(By.name("new_group"))).selectByVisibleText("group name 1");
		driver.findElement(By.name("address2")).clear();
		driver.findElement(By.name("address2")).sendKeys("194 s.hillview, Milpitas, CA, USA");
		driver.findElement(By.name("phone2")).clear();
		driver.findElement(By.name("phone2")).sendKeys("408-674-9669");
	}

	protected void findNew() {
		// FindNew
		driver.findElement(By.linkText("add new")).click();
	}

}
